/*     */ package da.factory;
/*     */ 
/*     */ import com.mysql.jdbc.Connection;
/*     */ import com.mysql.jdbc.Statement;
/*     */ import java.io.PrintStream;
/*     */ import java.sql.DriverManager;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.ResultSetMetaData;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Vector;
/*     */ import tools.MyLogger;
/*     */ 
/*     */ public class DBEngine
/*     */   implements IDBProperty
/*     */ {
/*  16 */   private Connection conn = null;
/*     */   private ResultSet rs;
/*  18 */   private boolean m_autoCommit = true;
/*     */ 
/*  61 */   private Statement stmt = null;
/*     */ 
/* 224 */   private int currentColumnCount = 0;
/*     */   private Vector<String> columns;
/*     */ 
/*     */   public DBEngine()
/*     */     throws Exception
/*     */   {
/*     */     try
/*     */     {
/*  22 */       Class.forName("com.mysql.jdbc.Driver").newInstance();
/*     */     }
/*     */     catch (Exception ex) {
/*  25 */       System.out.println(ex.getMessage());
/*  26 */       MyLogger.info(ex.getLocalizedMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public DBEngine(boolean autoCommit) throws Exception
/*     */   {
/*     */     try {
/*  33 */       Class.forName("com.mysql.jdbc.Driver").newInstance();
/*  34 */       this.m_autoCommit = autoCommit;
/*     */     }
/*     */     catch (Exception ex) {
/*  37 */       MyLogger.info(ex.getMessage());
/*  38 */       throw new Exception(ex.getMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public void commit()
/*     */   {
/*     */     try
/*     */     {
/*  46 */       this.conn.commit();
/*     */     }
/*     */     catch (SQLException ex) {
/*  49 */       MyLogger.info(ex.getLocalizedMessage());
/*     */     }
/*     */   }
/*     */ 
/*     */   public Connection getConnection()
/*     */   {
/*  58 */     return this.conn;
/*     */   }
/*     */ 
/*     */   public PreparedStatement getStatement(String strSQL)
/*     */     throws SQLException
/*     */   {
/*  65 */     PreparedStatement p = this.conn.prepareStatement(strSQL);
/*  66 */     return p;
/*     */   }
/*     */ 
/*     */   public boolean execute(String strSQL)
/*     */   {
/*     */     try {
/*  72 */       this.stmt = ((Statement)this.conn.createStatement());
/*  73 */       this.stmt.executeUpdate(strSQL);
/*  74 */       disConnect();
/*  75 */       return true;
/*     */     }
/*     */     catch (SQLException ex)
/*     */     {
/*  79 */       String exMsg = "SQLException: " + ex.getMessage() + "\r\n";
/*  80 */       exMsg = exMsg + "SQLState: " + ex.getSQLState() + "\r\n";
/*  81 */       exMsg = exMsg + "VendorError: " + ex.getErrorCode();
/*  82 */       System.out.println(exMsg);
/*  83 */       MyLogger.info(ex.getLocalizedMessage());
/*     */     }
/*  85 */     return false;
/*     */   }
/*     */ 
/*     */   public boolean connect()
/*     */   {
/*     */     try
/*     */     {
/*  92 */       this.conn = ((Connection)DriverManager.getConnection("jdbc:mysql://legenda/bcatransdb?autoReconnect=true&user=bcaconnect&password=theLapDanc3"));
/*     */ 
/*  95 */       this.conn.setAutoCommit(this.m_autoCommit);
/*  96 */       return true;
/*     */     }
/*     */     catch (SQLException ex) {
/*  99 */       String exMsg = "SQLException: " + ex.getMessage() + "\r\n";
/* 100 */       exMsg = exMsg + "SQLState: " + ex.getSQLState() + "\r\n";
/* 101 */       exMsg = exMsg + "VendorError: " + ex.getErrorCode();
/* 102 */       MyLogger.info(exMsg);
/* 103 */       System.out.println(exMsg);
/*     */     }
/* 105 */     return false;
/*     */   }
/*     */ 
/*     */   public boolean disConnect()
/*     */   {
/* 111 */     if (this.rs != null) {
/*     */       try {
/* 113 */         this.rs.close(); } catch (SQLException sqlEx) {
/*     */       }
/* 115 */       this.rs = null;
/*     */     }
/* 117 */     if (this.stmt != null) {
/*     */       try {
/* 119 */         this.stmt.close(); } catch (SQLException sqlEx) {
/*     */       }
/* 121 */       this.stmt = null;
/*     */     }
/* 123 */     return false;
/*     */   }
/*     */ 
/*     */   public Vector getData(String syntax)
/*     */   {
/* 130 */     MyLogger.info("getData->" + syntax);
/* 131 */     ResultSet rs = null;
/* 132 */     if (this.columns != null)
/* 133 */       this.columns = null;
/* 134 */     this.columns = new Vector();
/*     */ 
/* 136 */     Vector rows = new Vector();
/*     */     try {
/* 138 */       this.stmt = ((Statement)this.conn.createStatement());
/* 139 */       rs = this.stmt.executeQuery(syntax);
/* 140 */       int numOfCol = 0;
/*     */ 
/* 142 */       if (rs != null)
/*     */       {
/* 144 */         ResultSetMetaData rsmd = rs.getMetaData();
/* 145 */         numOfCol = rsmd.getColumnCount();
/* 146 */         this.currentColumnCount = numOfCol;
/*     */       }
/*     */ 
/* 149 */       while (rs.next())
/*     */       {
/* 151 */         Vector newRow = new Vector();
/* 152 */         for (int i = 0; i < numOfCol; i++)
/*     */         {
/* 154 */           int idx = i + 1;
/* 155 */           String colName = rs.getMetaData().getColumnName(idx);
/* 156 */           MyLogger.info("getData->col[" + idx + "]" + colName);
/* 157 */           this.columns.add(colName);
/* 158 */           String colType = rs.getMetaData().getColumnTypeName(idx);
/* 159 */           MyLogger.info("getData->coltype[" + idx + "]" + colType);
/* 160 */           if (colType.equals("VARBINARY"))
/*     */             try {
/* 162 */               String data = rs.getString(idx);
/* 163 */               MyLogger.info("getData->data[" + idx + "]" + data);
/* 164 */               newRow.addElement(data);
/*     */             }
/*     */             catch (Exception ex) {
/*     */             }
/*     */           else try {
/* 169 */               if ((rs.getObject(idx) == null) || (rs.wasNull())) {
/* 170 */                 newRow.addElement("");
/*     */               }
/*     */               else {
/* 173 */                 Object obj = rs.getObject(idx);
/* 174 */                 if (obj != null)
/*     */                 {
/* 176 */                   MyLogger.info("getData->data[" + idx + "]" + obj.toString());
/* 177 */                   newRow.addElement(obj);
/*     */                 }
/*     */                 else {
/* 180 */                   newRow.addElement("");
/*     */                 }
/*     */               }
/*     */             }
/*     */             catch (Exception ex)
/*     */             {
/*     */             }
/*     */         }
/* 188 */         rows.add(newRow);
/*     */       }
/*     */     }
/*     */     catch (SQLException ex)
/*     */     {
/* 193 */       String exMsg = "SQLException: " + ex.getMessage() + "\r\n";
/* 194 */       exMsg = exMsg + "SQLState: " + ex.getSQLState() + "\r\n";
/* 195 */       exMsg = exMsg + "VendorError: " + ex.getErrorCode();
/* 196 */       MyLogger.info("getData->ERROR->" + exMsg);
/* 197 */       System.out.println(exMsg);
/*     */     }
/*     */     finally
/*     */     {
/*     */     }
/*     */ 
/* 203 */     return rows;
/*     */   }
/*     */ 
/*     */   public boolean execute(PreparedStatement p)
/*     */   {
/*     */     try
/*     */     {
/* 211 */       p.executeUpdate();
/* 212 */       if (!this.m_autoCommit)
/* 213 */         this.conn.commit();
/* 214 */       return true;
/*     */     }
/*     */     catch (Exception ex)
/*     */     {
/* 218 */       MyLogger.info(ex.getLocalizedMessage());
/* 219 */       System.out.println(ex.getLocalizedMessage());
/*     */     }
/* 221 */     return false;
/*     */   }
/*     */ 
/*     */   public int getCurrentColumnCount()
/*     */   {
/* 228 */     return this.currentColumnCount;
/*     */   }
/*     */ 
/*     */   public String getColumnName(int idx)
/*     */   {
/* 234 */     return (String)this.columns.get(idx);
/*     */   }
/*     */ 
/*     */   public Vector getData(PreparedStatement p)
/*     */   {
/* 240 */     ResultSet rs = null;
/* 241 */     if (this.columns != null)
/* 242 */       this.columns = null;
/* 243 */     this.columns = new Vector();
/* 244 */     Vector rows = new Vector();
/*     */     try {
/* 246 */       rs = p.executeQuery();
/* 247 */       int numOfCol = 0;
/*     */       ResultSetMetaData rsmd;
/* 249 */       if (rs != null)
/*     */       {
/* 251 */         rsmd = rs.getMetaData();
/* 252 */         numOfCol = rsmd.getColumnCount();
/* 253 */         this.currentColumnCount = numOfCol;
/*     */       }
/*     */       else {
/* 256 */         rsmd = null;
        
/*     */       }
/* 258 */       while (rs.next())
/*     */       {
/* 260 */         Vector newRow = new Vector();
/* 261 */         for (int i = 0; i < numOfCol; i++)
/*     */         {
/* 263 */           this.columns.add(rs.getMetaData().getColumnName(i + 1));
/*     */ 
/* 265 */           if (rs.getMetaData().getColumnTypeName(i + 1).equals("VARBINARY")) {
/* 266 */             newRow.addElement(rs.getString(i + 1));
/*     */           }
/* 268 */           if (rs.getObject(i + 1) == null)
/* 269 */             newRow.addElement("");
/*     */           else {
/* 271 */             newRow.addElement(rs.getObject(i + 1));
/*     */           }
/*     */         }
/* 274 */         rows.add(newRow);
/*     */       }
/*     */     }
/*     */     catch (SQLException ex)
/*     */     {
/* 279 */       MyLogger.info(ex.getLocalizedMessage());
/* 280 */       String exMsg = "SQLException: " + ex.getMessage() + "\r\n";
/* 281 */       exMsg = exMsg + "SQLState: " + ex.getSQLState() + "\r\n";
/* 282 */       exMsg = exMsg + "VendorError: " + ex.getErrorCode();
/* 283 */       System.out.println(exMsg);
/*     */     }
/*     */     finally
/*     */     {
/*     */     }
/* 288 */     return rows;
/*     */   }
/*     */ }

/* Location:           F:\BCA.jar
 * Qualified Name:     da.factory.DBEngine
 * JD-Core Version:    0.6.0
 */